<?php
class model_exchange extends base_model
{
    /*
	* 取出指定ID分类
	* @param int $id 要取分类的ID
	*/
    public function mod_category_item($id){
        $sql = "SELECT * FROM `@@product_category` WHERE `id`={$id} LIMIT 1";
        $data = $this->db->query($sql,2);
        return $data;
    }
    /*
    * 取出所有的主分类
    * return array
    */
    public function mod_parentcategory(){
        $sql = "SELECT * FROM `@@product_category` WHERE `parentid`=0";
        $data = $this->db->query($sql,3);
        return $data;
    }
    /*
    * 取出所有的分类
    */
    public function mod_category_list($args){
        $where = '';
        $subwhere = '';
        $parentids = '';
        if(!empty($args['sKey'])){
            $where .= " AND `title` LIKE '%".strval($args['sKey'])."%'";
            $subwhere .= " AND `title` LIKE '%".strval($args['sKey'])."%'";
        }
        if(!empty($args['categoryid'])){
            $parentids .= ' AND `id`='.intval($args['categoryid']);
        }
        //找出主分类
        if(!empty($where)) {
            $sql = "SELECT GROUP_CONCAT(DISTINCT `parentid`) AS parentid FROM `@@product_category` WHERE 1 {$where}";
            $sd = $this->db->query($sql, 2);
            if($sd && !empty($sd['parentid'])){
                $parentids = ' AND `id` IN('.$sd['parentid'].')';
            }
        }

        $sql = "SELECT * FROM `@@product_category` WHERE `parentid`=0 {$parentids}";
        $data = $this->db->query($sql,3);
        foreach($data as $k=>$v){
            $subwhere .= ' AND `parentid`='.intval($v['id']);
            $data[$k]['subdata'] = $this->mod_sub_category_search($subwhere);
            $subwhere = '';
        }
        return $data;
    }
    /*
    * 取出指定（搜索）子分类
    * @param int $where
    * @param string $field 要返回的字段列表
    * return array
    */
    public function mod_sub_category_search($where,$field='*'){
        $sql = "SELECT {$field} FROM `@@product_category` WHERE 1 {$where} order by `parentid`,`sort` ASC";
        $data = $this->db->query($sql,3);
        return $data;
    }
    /*
    * 取出所有子分类
    * @param int $id 主分类的ID
    * @param string $field 要返回的字段列表
    * return array
    */
    public function mod_sub_category($id,$field='*'){
        $sql = "SELECT {$field} FROM `@@product_category` WHERE `parentid`={$id} order by `parentid`,`sort` ASC";
        $data = $this->db->query($sql,3);
        return $data;
    }
    /*
     * 商品列表
     */
    public  function mod_product_products($args){
        $where_txt = "";
        if(!empty($args['sKey'])){
            $where_txt .= " AND `title`='{$args['sKey']}'";
        }

        $sql = "SELECT ~field~ FROM `@@products` WHERE 1 {$where_txt} ~order~ ~limit~";
        //取出当前页的数据
        $sql_arr = array(
            'field' => '*',
            'page_current' => $args['page_current'],
            'pagesize' => $args['ps'],
            'order' => '',
        );
        $datas = $this->getTableRows($sql,$sql_arr);
        return array(
            'datas' => $datas['datas'],
            'record' => $datas['total'],
        );
    }
    # 用户VIP等级
    public function mod_user_vip()
    {
        $sql = "SELECT `lid`,`title` FROM `@@userlevel` ORDER BY `lid` ASC";
        $data = $this->db->query($sql,3);
        $args = array();
        foreach ($data as $v){
            $args[$v['lid']] = $v['title'];
        }
        return $args;
    }
    # 用户代理等级
    public function mod_agent_level()
    {
        $sql = "SELECT `lid`,`title` FROM `@@agentlevel` ORDER BY `lid` ASC";
        $data = $this->db->query($sql,3);
        $args = array();
        foreach ($data as $v){
            $args[$v['lid']] = $v['title'];
        }
        return $args;
    }
    # 单个商品
    public function mod_product_item($code)
    {
        $sql = "SELECT * FROM `@@products` WHERE `code`='{$code}' LIMIT 1";
        $data = $this->db->query($sql,2);
        $sql = "SELECT * FROM `@@products_con` WHERE  `code`='{$code}' LIMIT 1";
        $data['content'] = $this->db->query($sql,2);
        $data['content'] = $data['content']['content'];
        return $data;
    }
    # region 商品兑换订单列表
    public function mod_orders_list($args)
    {
        $where_txt = "";
        if(!empty($args['sKey'])){
            $where_txt .= " AND (u.`uno` LIKE '%".$args['sKey']."%' OR u.`nickname` LIKE '%".$args['sKey']."%' OR o.`orderid`='".$args['sKey']."')";
        }else{
            $fromTime = strtotime($args['fromTime']);
            $toTime = strtotime($args['toTime']) + 86400;
            //$where_txt .= " AND o.`actiontime` BETWEEN ".$fromTime." AND ".$toTime."";
        }

        $sql = "SELECT ~field~ FROM `@@users` u,`@@exchange_orders` o,`@@products` p WHERE u.`uid`=o.`uid` AND p.`code`=o.`proid`  {$where_txt} ~order~ ~limit~";
        //取出当前页的数据
        $sql_arr = array(
            'field' => 'u.uno,u.nickname,o.*,p.title,p.website,p.price,p.pricescore',
            'page_current' => $args['page_current'],
            'pagesize' => $args['ps'],
            'order' => ' ORDER BY o.`actiontime` DESC',
        );
        $datas = $this->getTableRows($sql,$sql_arr);
        return array(
            'datas' => $datas['datas'],
            'record' => $datas['total'],
        );
    }
    public function mod_orders_item($id)
    {
        $sql = "SELECT u.uno,u.nickname,o.*,p.title,p.website,p.price,p.pricescore FROM `@@users` u,`@@exchange_orders` o,`@@products` p WHERE o.`id`='{$id}' AND u.`uid`=o.`uid` AND p.`code`=o.`proid` LIMIT 1";
        $data = $this->db->query($sql,2);
        return $data;
    }
    # endregion

}